class Animal {
    public String name;
    public int age;
    public Animal(String name, int age ) {
        this.name = name;
        this.age = age;
    }
    public void eat() {
        System.out.println(name + "吃食物");
    }
}

class Cat extends Animal {
    public Cat(String name, int age) {
        super(name, age);
    }
    public void catchMouse() {
        System.out.println(name + "抓老鼠");
    }
    @Override
    public void eat() {
        System.out.println(name + "吃食物");
    }
}

class Dog extends Animal {

    public Dog(String name, int age) {
        super(name, age);
    }
    public void brak() {
        System.out.println(name + "汪汪叫");
    }
    @Override
    public void eat() {
        //与父类的eat方法构成重写
        System.out.println(name + "吃食物");
    }
}

public class TestAnimals {
    public static void main(String[] args) {
        Animal animal = new Dog("大黄", 7);
        if (animal instanceof Cat) {
            Cat cat = (Cat)animal;//向下转型
            cat.catchMouse();
        }
    }

    public static void main1(String[] args) {
        Animal animal = new Cat("小黑", 7);
        Cat cat = (Cat)animal;//向下转型
        cat.catchMouse();
    }



    // 方法传参：形参为父类型引用，可以接收任意子类的对象
    /*public static void eat(Animal animal) {
        animal.eat();
    }
    //作返回值：返回任意子类对象
    public static Animal byAnimals(String var) {
        if ("小狗狗".equals(var)) {
            return new Dog("小狗狗", 1);
        } else if ("小猫猫".equals(var)) {
            return new Cat("小猫猫", 1);
        }else {
            return null;
        }
    }
    public static void main(String[] args) {
        //直接赋值：子类对象赋值给父类对象
        Animal cat = new Cat("小黑", 7);
        Dog dog = new Dog("大黄", 6);
        eat(cat);
        eat(dog);

    }*/
}